<%-- 
/*
Assignment 1
Class ID        : SE0691
Group           : 1
Due Date        : 20 November 2012
I declare that this assignment is my own work
in accordance with FPT Policy.
*/
--%>

<%@page import="edu.fpt.hib.Apartment"%>
<%@page import="edu.fpt.hib.Users"%>
<%@page import="edu.fpt.dal.UsersDAL"%>
<%@page import="edu.fpt.hib.Services"%>
<%@page import="edu.fpt.dal.ServicesDAL"%>
<%@page import="edu.fpt.dal.ApartmentDAL"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    HttpSession newSession = request.getSession();
    if (newSession.getAttribute("currentAdmin") == null) {
        response.sendRedirect("../index.jsp");
    }
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Create New Users</title>
        <style type="text/css"> <!--@import url("../style.css");--></style>
        <script>function goBack()  {  window.history.back()  }</script>
    </head>
    <body>
        <jsp:include page="template/header.jsp"/>
        <div id="page">
            
            <% if (request.getParameter("username") == null) {%>
            <h2 class="title">Registration!</h2>
            <form method="POST" action="createuser.jsp">
                <table border="0">
                    <tr>
                        <td align="right">User ID:</td>
                        <td><input id="searchinput" type="text" name="username" size="20" required="required" maxlength="20"></td>
                    </tr>
                    <tr>
                        <td align="right">Password:</td>
                        <td><input id="searchinput" type="password" name="password" size="20" required="required" maxlength="16"></td>
                    </tr>
                    <tr>
                        <td align="right">Full Name:</td>
                        <td><input id="searchinput" type="text" name="fullname" size="20" required="required" maxlength="30"></td>
                    </tr>
                    <tr>
                        <td align="right">Using apartment:</td>
                        <td><select size="1" name="apartID">
                                <% ArrayList<String> list = ApartmentDAL.getListApartID();
                                    for (int i = 0; i < list.size(); i++) {%>
                                <option><%=list.get(i)%></option>
                                <%}%></select></td>
                    </tr>
                    <tr>
                        <td align="right">Ownership:</td>
                        <td><input type="radio" value="true" checked name="own" >Owner
                            <input type="radio" value="false"  name="own">Tenant</td>
                    </tr>
                    <tr>
                        <td align="right">Services:</td>
                        <td>
                            <% ArrayList<Services> service = ServicesDAL.listAll();
                                for (int i = 0; i < service.size(); i++) {%>
                            <input type="checkbox" name="<%=service.get(i).getServiceId()%>" value="ON"><%=service.get(i).getServiceName()%>
                            <%
                                if ((i + 1) % 4 == 0) {%>
                            <br>
                            <%}
                                }%>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">&nbsp;</td>
                        <td>
                            <p align="center"><input class="searchsubmit" type="submit" value="Register" name="B1"><input class="searchsubmit" type="reset" value="Reset" name="B2"></td>
                    </tr>
                </table>
            </form>
            <% } else {
                if (UsersDAL.exist(request.getParameter("username"))) {%>
            <h4>Duplicated username! Please choose another</h4><input id="searchsubmit" type='button' value='Back' onclick='goBack()'>
            <%} else {
                        String username = request.getParameter("username");
                        String password = request.getParameter("password");
                        String fullname = request.getParameter("fullname");
                        String usingService = "";
                        ArrayList<Services> service = ServicesDAL.listAll();
                        for (int i = 0; i < service.size(); i++) {
                            if (request.getParameter(service.get(i).getServiceId()) != null) {
                                usingService += service.get(i).getServiceId() + " ";
                            }
                        }
                        boolean own;
                        if (request.getParameter("own").equals("true")) {
                            own = true;
                        } else {
                            own = false;
                        }
                        String apartID = request.getParameter("apartID");
                        java.util.Date utilDate = new java.util.Date();
                        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
                        Users user = new Users(username, password, 2, fullname, sqlDate);
                        UsersDAL.insert(user);
                        Apartment apart = new Apartment(apartID, user, "Unavai", own, usingService, false, 0, 0);
                        ApartmentDAL.update(apart);%>
                        <h2 class="title">Successful</h2><br>
                        <a href="detailuser.jsp?userid=<%=user.getUserId()%>">View Created Users</a>
                        
                    <%}
                }   
            %>
        </div>
        <jsp:include page="template/footer.jsp"/>
    </body>
</html>
